import { baseRequestClient, requestClient } from '#/api/request';
import {router} from "#/router";
import {notification} from "ant-design-vue";
import {$t} from "@vben/locales";
import {encrypt} from "#/jwt/cryptoUtils";

export namespace RegisterALLApi {
  /** 注册接口参数 */
  export interface RegisterParams {
    password: string;
    username: string;
    agreePolicy: boolean;
    confirmPassword: string;
  }
}

/**
 * 用户注册接口
 */
export async function registerApi(data: RegisterALLApi.RegisterParams) {
  const encryptedValue = encrypt(JSON.stringify(data)); // 将 value 转换为字符串并加密
  const response = await requestClient.post('/auth/register', {
    encryptedData: encryptedValue,
  });
  /* eslint-disable no-console */
  console.log(response.code);
  /* eslint-disable no-console */
  if (response.code == 0) {
    // 重定向到登录页
    router.push('/auth/login');
    notification.success({
      message: $t(response.desc),
    });
    /* eslint-disable no-console */
  } else {
    // 重定向到注册页
    router.push('/auth/register');
    // throw new Error(response.desc);
    notification.error({
      message: $t(response.desc),
    });
    console.log("用户注册接口");
  }
}
